package org.makabaka.wfwwb.controller;

import org.makabaka.wfwwb.pojo.Result;
import org.makabaka.wfwwb.service.DeleteService;
import org.makabaka.wfwwb.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private DeleteService deleteService;

    @PostMapping("/{tableName}")
    public Result uploadExcel(@PathVariable String tableName, @RequestParam("file") MultipartFile file) {
        try {
            String res = uploadService.processExcel(tableName, file);
            return Result.success("文件上传成功:" + res);

        } catch (Exception e) {
            return Result.error("500", "文件上传失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/clear")
    public Result deleteAllData() {
        try {
            deleteService.deleteAll();
            return Result.success("成功删除所有表的数据");
        } catch (Exception e) {
            return Result.error("500", "删除失败: " + e.getMessage()) ;
        }
    }
}